package org.xxx.epms.gateway.config.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.xxx.epms.gateway.dto.AdminPermissionDto;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@NoArgsConstructor
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class AdminDetails implements UserDetails {

    private String adminCode;
    private String adminPassword;
    private List<AdminPermissionDto> adminResourcesList;

    public AdminDetails(String adminCode, String adminPassword, List<AdminPermissionDto> adminResourcesList) {
        this.adminCode = adminCode;
        this.adminPassword = adminPassword;
        this.adminResourcesList = adminResourcesList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return adminResourcesList.stream()
                .map(permission -> new SimpleGrantedAuthority("[Admin-Permission]" + permission.getAdminPermissionName() + "(" + permission.getAdminPermissionId() + ")"))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return this.adminPassword;
    }

    @Override
    public String getUsername() {
        return this.adminCode;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    @Override
    public String toString() {
        return "AdminDetails [adminCode=" + adminCode + ", adminPassword=" + adminPassword + ", adminResourcesList=" + adminResourcesList.toString() + "]";
    }
}